import { BasicColumn } from '@/components/Table';
import { FormSchema } from '@/components/Table';
import { cacheApi } from '@/api/sys/common';
import { getTableList } from '@/api/sys/system';
import { ref } from 'vue';
export const columns: BasicColumn[] = [
  {
    title: '序号名称',
    align: 'center',
    dataIndex: 'name',
  },
  {
    title: '数据源',
    align: 'center',
    dataIndex: 'datasource',
  },
  {
    title: '表名称',
    align: 'center',
    dataIndex: 'tableName',
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime',
  },
  {
    title: '创建人',
    align: 'center',
    dataIndex: 'createUserStr',
  },
  {
    title: '更新时间',
    align: 'center',
    dataIndex: 'updateTime',
  },
  {
    title: '更新人',
    align: 'center',
    dataIndex: 'updateUserStr',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'tableName',
    label: '表名',
    component: 'Input',
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'name',
    label: '序号名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'datasource',
    label: '数据源',
    component: 'ApiSelect',
    componentProps: {
      showSearch: true,
      api: cacheApi,
      params: { keyCode: 'datasource' },
    },
    required: true,
  },
  {
    field: 'tableName',
    label: '表名称',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formModel }) => {
      return {
        showSearch: true,
        api: getTableList,
        valueField: 'tableName',
        labelField: 'tableName',
        params: { dataSourceCode: formModel.datasource },
      };
    },
  },
];

export const subColumns: BasicColumn[] = [
  {
    title: '序号类型',
    align: 'center',
    dataIndex: 'typeStr',
  },
  {
    title: '编码规则',
    align: 'center',
    dataIndex: 'code',
  },
  {
    title: '配置项',
    align: 'center',
    dataIndex: 'config',
  },
  {
    title: '排序',
    align: 'center',
    dataIndex: 'sort',
  },
];
const options = ref([]);
export const subFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'configId',
    label: '',
    component: 'Input',
    show: false,
  },
  {
    field: 'type',
    label: '序号类型',
    component: 'ApiSelect',
    componentProps: ({ formModel }) => {
      return {
        dictCode: 'sequence_type',
        onChange(d, emitData) {
          if (d && emitData && d !== 'regular') {
            options.value = JSON.parse(emitData.data.ext1);
          } else {
            formModel.secdTimeZoon = undefined;
            formModel.secdFormat = undefined;
          }
        },
      };
    },
    required: true,
  },
  {
    field: 'code',
    label: '编码规则',
    component: 'Input',
    ifShow: ({ values }) => values.type === 'regular',
    required: true,
  },
  {
    field: 'code',
    label: '编码规则',
    component: 'Select',
    ifShow: ({ values }) => values.type !== 'regular',
    componentProps: () => {
      return {
        options: options.value,
      };
    },
    required: true,
  },
  {
    field: 'config',
    label: '配置项',
    component: 'Input',
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
];
